package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {
    private String recAreaInfo;//收件人省市区信息
    private String sendAreaInfo; //寄件人省市区信息
    
   public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

@Action(value="order_add",results={@Result(name="success",type="redirect",location="index.html")}) 
    public String orderAdd(){
	   //切割数据
	   String[] strs= sendAreaInfo.split("/");
	   //封装寄件人地址数据
	   Area  sendArea=new  Area();
	   sendArea.setProvince(strs[0]);
	   sendArea.setCity(strs[1]);
	   sendArea.setDistrict(strs[2]);
	   model.setSendArea(sendArea);
	   //切割数据
	   String[] strs1= recAreaInfo.split("/");
	   //封装收件人地址数据
	   Area recArea=new Area();
	   recArea.setProvince(strs1[0]);
	   recArea.setCity(strs1[1]);
	   recArea.setDistrict(strs1[2]); 
	   model.setRecArea(recArea);
	   //关联客户
	  Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
	  model.setCustomer_id(customer.getId()); 
	  
	  WebClient
		.create(Constants.BOS_MANAGEMENT_URL
				+ "/services/orderService/order")
		.type(MediaType.APPLICATION_JSON).post(model);
	   return SUCCESS;
    }
	

}
